package gui.screens;

import gui.CanvasManager;

import java.io.IOException;
import java.util.Vector;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import application.MainController;

import domain.Hunter;

/**
 * Class for the highscore screen.
 * @author Sandra Mayer
 *
 */
public class HighscoreScreen extends Canvas implements CommandListener {
	private static final String FILENAME = "/mainWhite.jpg";

	private Image backgroundImg;
	private Command okCmd;
	private Vector /*Hunter*/ hunter;
	private static HighscoreScreen instance;
	
	/**
	 * Constructor.
	 */
	private HighscoreScreen() {
		try {
			backgroundImg = Image.createImage(FILENAME);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		okCmd = new Command("Ok", Command.OK, 1);
		this.addCommand(okCmd);
		this.setCommandListener(this);
	}

	/**
	 * Gets the instance of the Highscore screen.
	 * @return HighscoreScreen
	 */
	public static HighscoreScreen getInstance() {
		if (instance == null)
			instance = new HighscoreScreen();
		CanvasManager.setCurScreen(instance);
		instance.hunter = MainController.getInstance().getHighscore();
		return instance;
	}
	
	/**
	 * Paints the screen.
	 */
	protected void paint(Graphics g) {
		g.setColor(0xffffff);
		g.fillRect(0, 0, getWidth(), getHeight());
		g.translate((getWidth() - backgroundImg.getWidth()) / 2,
				(getHeight() - backgroundImg.getHeight()) / 2);

		g.drawImage(backgroundImg, 0, 0, Graphics.TOP | Graphics.LEFT);
		
		g.setColor(0x000000);
		
		g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD,
				Font.SIZE_MEDIUM));
		
		g.drawString("Pos", 15, 50, Graphics.LEFT | Graphics.TOP);
		g.drawString("Name", 45, 50, Graphics.LEFT | Graphics.TOP);
		g.drawString("Kills", 165, 50, Graphics.LEFT | Graphics.TOP);
		
		if (hunter != null) {
			int x = 15;
			int y = 70;
			Hunter h = null;
			for (int i = 0; i < hunter.size(); i++) {
				h = (Hunter)hunter.elementAt(i);
				g.drawString((i + 1) + "", x, y, Graphics.LEFT | Graphics.TOP);
				x += 30;
				g.drawString(h.getName(), x, y, Graphics.LEFT | Graphics.TOP);
				x += 120;
				g.drawString(h.getKills() + "", x, y, Graphics.LEFT | Graphics.TOP);
				x = 15;
				y += 25;
			}
		}
	}

	/**
	 * Does the command action.
	 */
	public void commandAction(Command cmd, Displayable arg1) {
		if (cmd == okCmd) {
			CanvasManager.showCanvas();
		}
	}

}
